//
//  CSafeMeetAndSendCell.m
//  ChildHoodStemp
//
//  Created by lyt on 14-12-20.
//
//
#define KViewTagBase 1000
#import "CSafeMeetAndSendCell.h"
#import "UIUtil.h"
#import "MutiltimediaView.h"
#import "CHImageScanView.h"
#import "MessageDao.h"
#import "Snsp.pb.h"
#import "MicroDef.h"
#import "NSData+CBExtension.h"

@implementation CSafeMeetAndSendCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.imageUrl = nil;
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

//-(void)setDefault
//{
//    self.backgroundColor = [UIColor clearColor];
//    
//    UIView *backGroundView = [self viewWithTag:KViewTagBase+0];
//    if(backGroundView == nil)
//    {
//        backGroundView = [[UIView alloc] initWithFrame:CGRectMake(10, 5, 300, 100.5)];
//        backGroundView.backgroundColor = [UIColor whiteColor];
//        backGroundView.tag = KViewTagBase + 0;
//        backGroundView.layer.cornerRadius = 12;
//        backGroundView.layer.masksToBounds = YES;
//        [self addSubview:backGroundView];
//    
//    }
//    
//    UIImageView *headerLabelBack = (UIImageView *)[self viewWithTag:KViewTagBase + 1];
//    if(headerLabelBack == nil)
//    {
//        headerLabelBack = [[UIImageView alloc] initWithFrame:CGRectMake(17, 12, 286, 21)];
//        [headerLabelBack setImage:[UIImage imageNamed:@"safe_meetAndSendTitleBg.png"]];
//        headerLabelBack.tag = KViewTagBase + 1;
//        [self addSubview:headerLabelBack];
//    }
//    
//    UILabel *headerLabel = (UILabel *)[self viewWithTag:KViewTagBase + 2];
//    if(headerLabel == nil)
//    {
//        headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(17, 12, 286, 21)];
//        headerLabel.tag = KViewTagBase+2;
//        [headerLabel setBackgroundColor:[UIColor clearColor]];
//        [headerLabel setTextColor:[UIColor whiteColor]];
//        [headerLabel setFont:[UIFont systemFontOfSize:12]];
//        [self addSubview:headerLabel];
////        self.titleLabel = headerLabel;
//    }
//    if(headerLabel)
//    {
//        headerLabel.text = @"  刷卡通知   2014年12月21号    08：35     星期一";
//    }
//    
////    UIImageView *photoImage = (UIImageView *)[self viewWithTag:KViewTagBase + 3];
////    if(photoImage == nil)
////    {
////        photoImage = [[UIImageView alloc] initWithFrame:CGRectMake(17, 40.5, 77, 58)];
////        [photoImage setImage:[UIImage imageNamed:@"safe_meetAndSend_defaultImage.png"]];
////        photoImage.tag = KViewTagBase + 3;
////        [self addSubview:photoImage];
////    }
////
////    _portraitView = [[MutiltimediaView alloc] initWithFrame:CGRectMake(HorSpace, VerSpace, PortraitEdge, PortraitEdge)] ;
////    [_portraitView setBackgroundColor:ClearColor];
////    [_portraitView setClipsToBounds:YES];
////    [_portraitView.layer setCornerRadius:10];
//    MutiltimediaView *photoImage = (MutiltimediaView *)[self viewWithTag:KViewTagBase + 3];
//    if(photoImage == nil)
//    {
//        photoImage = [[MutiltimediaView alloc] initWithFrame:CGRectMake(17, 40.5, 77, 58)];
//        [photoImage setImage:[UIImage imageNamed:@"safe_meetAndSend_defaultImage.png"]];
//        self.imageUrl = @"safe_meetAndSend_defaultImage.png";
//        photoImage.tag = KViewTagBase + 3;
//        [photoImage setBackgroundColor:ClearColor];
//        [photoImage setClipsToBounds:YES];
//        [photoImage.layer setCornerRadius:10];
//        photoImage.isAutoPlayOrShow = YES;
////        photoImage.isFromChat = YES;
//        [self addSubview:photoImage];
//        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pictureBubbleViewTapEvent:)];
//        tap.numberOfTapsRequired = 1;
//        tap.numberOfTouchesRequired = 1;
//        [photoImage addGestureRecognizer:tap];
//    }
//    
//    
//    UILabel *contentLabel = (UILabel *)[self viewWithTag:KViewTagBase + 4];
//    if(contentLabel == nil)
//    {
//        contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(101, 40.5, 182, 58)];
//        contentLabel.tag = KViewTagBase+4;
//        contentLabel.numberOfLines = 4;
//        [contentLabel setFont:[UIFont systemFontOfSize:12]];
//        [contentLabel setTextColor:SpecialGrayColor];
//        [self addSubview:contentLabel];
////        self.titleLabel = contentLabel;
//    }
//    if(contentLabel)
//    {
//        contentLabel.text = @"姓名: 王天来\r\n卡号:123456789\r\n身份:爸爸\r\n班级:土星阳光班";
//        [contentLabel sizeToFit];
//    }
//    
//
//}

-(void)setMessageData:(MessageDBModel *)msgDBModel
{
    
    NSData *data = [msgDBModel.messageContent dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dic = [data jsonObject];
    

    self.backgroundColor = [UIColor clearColor];
    
    UIView *backGroundView = [self viewWithTag:KViewTagBase+0];
    if(backGroundView == nil)
    {
        backGroundView = [[UIView alloc] initWithFrame:CGRectMake(10, 5, HARDWARE_SCREEN_WIDTH - 20, 100.5+7)];
        backGroundView.backgroundColor = [UIColor whiteColor];
        backGroundView.tag = KViewTagBase + 0;
        backGroundView.layer.cornerRadius = 12;
        backGroundView.layer.masksToBounds = YES;
        [self addSubview:backGroundView];
        
    }
    
    UIImageView *headerLabelBack = (UIImageView *)[self viewWithTag:KViewTagBase + 1];
    if(headerLabelBack == nil)
    {
        UIImage *headerLabelImage = [UIImage imageNamed:@"safe_meetAndSendTitleBg.png"];
        headerLabelImage = [headerLabelImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 20)];
        headerLabelBack = [[UIImageView alloc] initWithFrame:CGRectMake(17, 12, HARDWARE_SCREEN_WIDTH - 34, 21)];
        [headerLabelBack setImage:headerLabelImage];
        headerLabelBack.tag = KViewTagBase + 1;
        [self addSubview:headerLabelBack];
    }
    
    UILabel *headerLabel = (UILabel *)[self viewWithTag:KViewTagBase + 2];
    if(headerLabel == nil)
    {
        headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(17, 12, HARDWARE_SCREEN_WIDTH - 34, 21)];
        headerLabel.tag = KViewTagBase+2;
        [headerLabel setBackgroundColor:[UIColor clearColor]];
        [headerLabel setTextColor:[UIColor whiteColor]];
        [headerLabel setFont:[UIFont systemFontOfSize:12]];
        [self addSubview:headerLabel];
        //        self.titleLabel = headerLabel;
    }
    if(headerLabel)
    {
//        headerLabel.text = @"  刷卡通知   2014年12月21号    08：35     星期一";
        headerLabel.text = [self getHeaderText:[dic objectForKey:@"时间"]];
    }
    
    UILabel *weekLabel = (UILabel *)[self viewWithTag:KViewTagBase + 3];
    if(weekLabel == nil)
    {
        weekLabel = [[UILabel alloc] initWithFrame:CGRectMake(257, 12, 40, 21)];
        weekLabel.tag = KViewTagBase+3;
        [weekLabel setBackgroundColor:[UIColor clearColor]];
        [weekLabel setTextColor:[UIColor whiteColor]];
        [weekLabel setFont:[UIFont systemFontOfSize:12]];
        [self addSubview:weekLabel];
//        weekLabel.text = @"星期一";
        //        self.titleLabel = headerLabel;
    }
    if(weekLabel != nil)
    {
        weekLabel.text = [self getWeekNameByTime:[dic objectForKey:@"时间"]];
    }
    
    
    MutiltimediaView *photoImage = (MutiltimediaView *)[self viewWithTag:KViewTagBase + 4];
    if(photoImage == nil)
    {
        photoImage = [[MutiltimediaView alloc] initWithFrame:CGRectMake(17, 40.5+3.5, 77, 58)];
//        [photoImage setImage:[UIImage imageNamed:@"safe_meetAndSend_defaultImage.png"]];

        photoImage.tag = KViewTagBase + 4;
        [photoImage setBackgroundColor:ClearColor];
        [photoImage setClipsToBounds:YES];
        [photoImage.layer setCornerRadius:10];
        photoImage.isAutoPlayOrShow = YES;
        //        photoImage.isFromChat = YES;
        [self addSubview:photoImage];
        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pictureBubbleViewTapEvent:)];
        tap.numberOfTapsRequired = 1;
        tap.numberOfTouchesRequired = 1;
        [photoImage addGestureRecognizer:tap];
    }
    if(photoImage)
    {
        NSString *photoUrl = [dic objectForKey:@"附件"];
        if(photoUrl == nil || [photoUrl length] == 0)
        {
            photoUrl = @"safe_meetAndSend_defaultImage.png";
            [photoImage setImage:[UIImage imageNamed:photoUrl]];
        }
        else
        {
//            [photoImage setMultimediaFileUri:photoUrl type:SNSPMaterialTypeKImageMessage];
        }
        self.imageUrl = photoUrl;
//        [photoImage setImage:[UIImage imageNamed:photoUrl]];
//-(void)setMultimediaFileUri:(NSString*)fileUri type:(int32_t)type;
        
    }
    
    
    
    UILabel *contentLabel = (UILabel *)[self viewWithTag:KViewTagBase + 5];
    if(contentLabel == nil)
    {
        contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(101, 40.5-1.5, 182, 58+7+3)];
        contentLabel.tag = KViewTagBase+5;
        contentLabel.numberOfLines = 4;
//        [contentLabel setFont:[UIFont systemFontOfSize:12]];
        [contentLabel setFont:[UIFont boldSystemFontOfSize:14]];
        [contentLabel setTextColor:SpecialGrayColor];
        [self addSubview:contentLabel];
        //        self.titleLabel = contentLabel;
    }
    if(contentLabel)
    {
//        contentLabel.text = @"姓名: 王天来\r\n卡号:123456789\r\n身份:爸爸\r\n班级:土星阳光班";
        contentLabel.text = [self getContentText:dic];
//        [contentLabel sizeToFit];
    }
    


}


-(NSString *)getHeaderText:(NSString *)ServerDateStr
{
    
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *cardTime = [format dateFromString:ServerDateStr];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = nil ;
    NSInteger unitFlags = NSYearCalendarUnit |
    NSMonthCalendarUnit |
    NSDayCalendarUnit |
    NSWeekdayCalendarUnit |
    NSHourCalendarUnit |
    NSMinuteCalendarUnit |
    NSWeekCalendarUnit |
    NSSecondCalendarUnit;
    comps = [calendar components:unitFlags fromDate:cardTime];
    
    
    NSString *formatTime = nil;
    
    if([comps second] != 0)
    {
        formatTime = [NSString stringWithFormat:@"  刷卡通知      %4d年%02d月%02d号    %02d:%02d:%02d",[comps year], [comps month], [comps day], [comps hour], [comps minute], [comps second]];
    }
    else
    {
        formatTime = [NSString stringWithFormat:@"  刷卡通知      %4d年%02d月%02d号    %02d:%02d",[comps year], [comps month], [comps day], [comps hour], [comps minute]];
    }
    
    
    return formatTime;
}


-(NSString *)getWeekNameByTime:(NSString *)ServerDateStr
{
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *cardTime = [format dateFromString:ServerDateStr];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = nil;
    
    NSInteger unitFlags = NSYearCalendarUnit |
    NSMonthCalendarUnit |
    NSDayCalendarUnit |
    NSWeekdayCalendarUnit |
    NSHourCalendarUnit |
    NSMinuteCalendarUnit |
    NSWeekCalendarUnit |
    NSSecondCalendarUnit;
    comps = [calendar components:unitFlags fromDate:cardTime];
    
    
    NSString *formatTime = nil;
    
    formatTime = [self getWeekName:[comps weekday]];
    
    return formatTime;


}

-(NSString *)getWeekName:(NSInteger)index
{
    NSString *weekName = nil;
    switch (index) {
        case 1:
            weekName = @"星期日";
            break;
        case 2:
            weekName = @"星期一";
            break;
        case 3:
            weekName = @"星期二";
            break;
        case 4:
            weekName = @"星期三";
            break;
        case 5:
            weekName = @"星期四";
            break;
        case 6:
            weekName = @"星期五";
            break;
        case 7:
            weekName = @"星期六";
            break;
        default:
            break;
    }
    return weekName;
}



-(NSString *)getContentText:(NSDictionary *)dic
{
//    contentLabel.text = @"姓名: 王天来\r\n卡号:123456789\r\n身份:爸爸\r\n班级:土星阳光班";
    NSString *contentText = [NSString stringWithFormat:@"姓名: %@\r\n卡号: %@\r\n身份: %@\r\n班级: %@", [dic objectForKey:@"姓名"],[dic objectForKey:@"卡号"],  [self getIdentityName:dic],  [self getPartName:dic]];

    return contentText;
}

-(NSString *)getIdentityName:(NSDictionary *)dic
{
    NSString *name = [dic objectForKey:@"身份"];
    if(name == nil || [name length] <= 0)
    {
        name = [dic objectForKey:@"职位"];
    }
    return name;
}
-(NSString *)getPartName:(NSDictionary *)dic
{
    NSString *name = [dic objectForKey:@"班级"];
    if(name == nil || [name length] <= 0)
    {
        name = [dic objectForKey:@"部门"];
    }
    return name;
}

-(void)pictureBubbleViewTapEvent:(id)sender
{
//    [CHImageScanView sharedInstance].chMessages = self.messageList;
//    [CHImageScanView sharedInstance].imageArray = nil;
//    if(self.largePicURI != nil)
    {
//        [[CHImageScanView sharedInstance] showImgWithURI:@"safe_meetAndSend_defaultImage.png"];
//        [[CHImageScanView sharedInstance] showImgWithURI: self.imageUrl];
    }
}

@end
